import { EnterpriseApi } from '@/api';
import { mapGetters } from 'vuex';
export default {
  name: 'EnterpriseDetail',
  data() {
    return {
      companyInfo: {
        id: '',
        name: '',
        address: '',
        contact_person: '',
        contact_phone: '',
        establish_time: ''
      },
      // 证书信息
      certificates: []
    };
  },
  computed: {
    ...mapGetters(['user']),
  },
  mounted() {
    if (this.$route.query.id) {
      this.fetchDetailData();
    }
  },
  methods: {
    async fetchDetailData() {
      const response = await EnterpriseApi.detail(this.$route.query.id);
      this.companyInfo = response.result.company_info;
      this.certificates = response.result.certificates
    },
    async onSubmitEnterpriseInfo() {
      if (this.$route.query.id) {
        this.onUpdateEnterpriseInfo();
      } else {
        this.onAddEnterpriseInfo()
      }
    },
    async onUpdateEnterpriseInfo() {
      try {
        await EnterpriseApi.edit(this.$route.query.id, this.companyInfo)
        this.$message.success('操作成功')
        this.$router.back()
      } catch (response) {
        this.$message.error('操作失败：' + response.msg)
      }
    },
    async onAddEnterpriseInfo() {
      try {
        const response = await EnterpriseApi.add(this.companyInfo)
        this.$message.success('操作成功')
        this.$router.replace({ path: '/enterprise/detail', query: {id: response.result.company_id }})
      } catch (response) {
        this.$message.error('操作失败：' + response.msg)
      }
    }
  },
};
